#include "headfiles.h"

bool calc3OrderedNumbers(int *a, int n, int &res_i, int &res_j, int &res_k) {
    if (a == NULL ||
        n < 3) {
        return false;
    }

    bool *flag = (bool *)malloc(sizeof(bool) * n);
    if (flag == NULL) {
        return false;
    }

    memset(flag, 0, sizeof(bool));

    //Find all element that has a smaller number than it
    int i, min_val = a[0], index = -1;
    for (i = 1; i < n; i++) {
        if (a[i] > min_val) {
            flag[i] = true;
        }

        if (a[i] < min_val) {
            min_val = a[i];
        }
    }

    int max_val = a[n - 1];
    for (i = n - 1; i >= 0; i--) {
        if (a[i] < max_val) {
            flag[i] = flag[i] && true;
            if (flag[i]) {
                index = i;
            }
        }
        else {
            flag[i] = false;
        }

        if (a[i] > max_val) {
            max_val = a[i];
        }
    }

    if (index != -1) {
        res_j = index;
        for (i = 0; i < index; i++) {
            if (a[i] < a[index]) {
                res_i = i;
                break;
            }
        }

        for (i = index + 1; i < n; i++) {
            if (a[i] > a[index]) {
                res_k = i;
                break;
            }
        }

        return true;
    }
    else {
        return false;
    }
}

